<?php require_once 'inc/header.php'; ?>
<?php

$categories = $db->q('SELECT category, COUNT(*) AS likes FROM likes WHERE user_fbid = ? GROUP BY category ORDER BY likes DESC', $_SESSION['me']['fbid']);
$groupRows = array();
foreach ($categories as $c) $groupRows[] = array($c['category'], (int)$c['likes']);

$friends = $db->q('
	SELECT fbid, name
	FROM users
	WHERE fbid IN (SELECT friend_fbid FROM friends WHERE user_fbid = ?)
	ORDER BY name
', $_SESSION['me']['fbid']);

$popularFriendLikes = $db->q('
	SELECT title, COUNT(*) AS count FROM likes WHERE user_fbid IN (SELECT friend_fbid FROM friends WHERE user_fbid = ?) GROUP BY object_fbid, title ORDER BY count DESC LIMIT 10
', $_SESSION['me']['fbid']);

$pflRows = array();
$pflMax = 0;
foreach ($popularFriendLikes as $l) {
	$count = (int)$l['count'];
	$pflMax = $count > $pflMax ? ceil($count / 4) * 4 : $pflMax;
	$pflRows[] = array($l['title'], $count);
}

?>

<div id="compareLikesContainer">
	Compare with 
	<select id="compareLikesWith">
		<option value="0">None</option>
		<?php foreach ($friends as $f) printf('<option value="%s">%s</option>', htmlentities($f['fbid']), htmlentities($f['name'])); ?>
	</select>
</div>
<div id="chart_div"></div>
<div id="friendLikesChart"></div>
<div id="pflChart"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
	var data = new google.visualization.DataTable();
	data.addColumn('string', 'Category');
	data.addColumn('number', 'No. of Likes');
	data.addRows(<?php echo json_encode($groupRows); ?>);

	var options = {
	  height: 400,
	  title: 'My Likes'
	};

	var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
	chart.draw(data, options);
  }

	$('#compareLikesWith').change(function() {
		var fbid = +$(this).val();
		console.log('#compareLikesWith change event occurred', fbid);
		if (fbid) {
			$('#notice').text('Pulling like data...').slideDown('fast');
			$.get('like_data.php?signed_request=<?php if (!empty($_POST['signed_request'])) echo $_POST['signed_request']; ?>&fbid=' + fbid, function(data) {
				var table = new google.visualization.DataTable();
				table.addColumn('string', 'Category');
				table.addColumn('number', 'No. of Likes');
				table.addRows(data.categories);
				new google.visualization.PieChart(document.getElementById('friendLikesChart')).draw(table, {
					height: 400,
					title: data.name + "'s Likes"
				});
				$('#notice').slideUp('fast', function() {
					$(this).empty();
				});
			});
		} else {
			$('#friendLikesChart').empty();
		}
	});
	
	var pflData = new google.visualization.DataTable();
	pflData.addColumn('string', 'Place');
	pflData.addColumn('number', 'Friends');
	pflData.addRows(<?php echo json_encode($pflRows); ?>);
	var pflChart = new google.visualization.BarChart(document.getElementById('pflChart'));
	pflChart.draw(pflData, {title: 'Popular Friend Likes - Top 10', chartArea: {left:200}, hAxis:{minValue: 0, maxValue: <?php echo $pflMax; ?>}});
</script>
<?php require_once 'inc/footer.php'; ?>